package com.zhuss.food.order;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.order.DetailOrderBo;
import com.zhuss.bean.bo.food.order.SearchOrderBo;
import com.zhuss.bean.bo.food.order.wechat.ListOrderBo;
import com.zhuss.bean.vo.core.company.DetailCompanyVo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.food.order.DetailOrderVo;
import com.zhuss.bean.vo.food.order.ListOrderVo;
import com.zhuss.bean.vo.food.order.box.ListBoxOrderVo;
import com.zhuss.bean.vo.food.order.stirfry.ListStirfryOrderVo;
import com.zhuss.bean.vo.food.order.takeout.ListTakeoutOrderVo;
import com.zhuss.bean.vo.food.order.wechat.OrderVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.ReadService;
import com.zhuss.core.processor.CompanyRead;
import com.zhuss.core.processor.EnumConfig;
import com.zhuss.core.processor.UserRead;
import com.zhuss.exception.WpsException;
import com.zhuss.food.processor.FoodRead;
import com.zhuss.food.processor.OrderRead;


/**
 * 订单查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020/03/09 16:01
 *
 */
@Component("orderReadService")
public class OrderReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("orderRead")
	private OrderRead orderRead;
	
	@Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
	
	@Autowired
	@Qualifier("userRead")
	private UserRead userRead;
	
	@Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;
	
	@Autowired
	@Qualifier("enumConfig")
	private EnumConfig enumConfig;
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailOrderBo bo = (DetailOrderBo) param;
		Long orderId = bo.getOrderId();
		DetailOrderVo vo = (DetailOrderVo) orderRead.detail(orderId);
		return vo;
	}
	
	/**
	 * 公务接待
	 * 列表
	 * @throws WpsException 
	 */
	@SuppressWarnings("unchecked")
	public Object boxList(Object param) throws WpsException {
		SearchOrderBo bo = (SearchOrderBo) param;
		PageVo<ListOrderVo> pageVo = (PageVo<ListOrderVo>) list(bo);
		PageVo<ListBoxOrderVo> result = new PageVo<ListBoxOrderVo>();
		List<ListBoxOrderVo> list = new ArrayList<ListBoxOrderVo>();
		BeanUtils.copyProperties(pageVo, result);
		for (ListOrderVo item : pageVo.getItems()) {
			ListBoxOrderVo box = new ListBoxOrderVo();
			BeanUtils.copyProperties(item, box);
			list.add(box);
		}
		result.setItems(list);
		return result;
	}
	
	/**
	 * 点炒服务
	 * 列表
	 * @throws WpsException 
	 */
	@SuppressWarnings("unchecked")
	public Object stirfryList(Object param) throws WpsException {
		SearchOrderBo bo = (SearchOrderBo) param;
		PageVo<ListOrderVo> pageVo = (PageVo<ListOrderVo>) list(bo);
		PageVo<ListStirfryOrderVo> result = new PageVo<>();
		List<ListStirfryOrderVo> list = new ArrayList<>();
		BeanUtils.copyProperties(pageVo, result);
		for (ListOrderVo item : pageVo.getItems()) {
			ListStirfryOrderVo stirfry = new ListStirfryOrderVo();
			BeanUtils.copyProperties(item, stirfry);
			list.add(stirfry);
		}
		result.setItems(list);
		return result;
	}
	
	/**
	 * 外卖打包
	 * 列表
	 * @throws WpsException 
	 */
	@SuppressWarnings("unchecked")
	public Object takeoutList(Object param) throws WpsException {
		SearchOrderBo bo = (SearchOrderBo) param;
		PageVo<ListOrderVo> pageVo = (PageVo<ListOrderVo>) list(bo);
		PageVo<ListTakeoutOrderVo> result = new PageVo<>();
		List<ListTakeoutOrderVo> list = new ArrayList<>();
		BeanUtils.copyProperties(pageVo, result);
		for (ListOrderVo item : pageVo.getItems()) {
			ListTakeoutOrderVo takeout = new ListTakeoutOrderVo();
			BeanUtils.copyProperties(item, takeout);
			list.add(takeout);
		}
		result.setItems(list);
		return result;
	}
	
	/**
	 * 我的订单
	 * 列表
	 * @throws WpsException 
	 */
	public List<OrderVo> listWechat(ListOrderBo param) throws WpsException {
		List<OrderVo> result = orderRead.listOrder(param);
		return result;
	}
	
	/**
	 * 我的订单
	 * 详情
	 * @throws WpsException 
	 */
	public OrderVo detailWechat(DetailOrderBo param) throws WpsException {
		OrderVo result = orderRead.detailOrder(param);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Object list(Object param) throws WpsException {
		SearchOrderBo bo = (SearchOrderBo) param;
		PageVo<ListOrderVo> result = (PageVo<ListOrderVo>) orderRead.list(bo);
		transferToVo(result.getItems());
		return result;
	}
	
	private void transferToVo(List<ListOrderVo> orderList) throws WpsException {
		for (ListOrderVo item : orderList) {
			Long userId = item.getCreatorId();
			DetailUserVo user = (DetailUserVo) userRead.detail(userId);
			String phone = user.getPhone();
			Long companyId = user.getCompanyId();
			DetailCompanyVo company = (DetailCompanyVo) companyRead.detail(companyId);
			String companyName = company.getName();
			item.setPhone(phone);
			item.setCompanyName(companyName);
		}
	}
	
	
}

